package sy.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import sy.jackson.IotParkingURIProjectMappingMixin;
import sy.model.IotParkingURI;
import sy.model.IotParkingURIProjectMapping;
import sy.model.SysProject;
import sy.service.IotParkingURIProjectMappingServiceI;
import sy.service.IotParkingURIServiceI;
import sy.service.SysProjectServiceI;
import sy.util.MessageObject;


@Controller
@RequestMapping("iotParkingURIProjectMappingController")
public class IotParkingURIProjectMappingController {
	@Autowired
	IotParkingURIProjectMappingServiceI iotParkingURIProjectMappingService;
	@Autowired
	IotParkingURIServiceI iotParkingURIService;
	@Autowired
	SysProjectServiceI sysProjectService;
	
	private static final ObjectMapper objectMapper = new ObjectMapper();
	
	/************************** view controller ***************************/

	/**
	 * 
		 * @Title      : objectView
		 * @Description: 首页视图
		 * @param      : model
		 * @return     : String
		 * @author     : jiayiwu
		 * @time       : 2017年8月30日 
		 * @throws
	 */
	@RequestMapping(value="/view")
	public String objectView(Model model){
	
		return "iotInterface/parkingURI/parkingURIPrejectMapping";
	}
	
	/**
	 * 
		 * @Title      : newObjectInfo
		 * @Description:  TODO
		 * @param      :@param model
		 * @param      :@return
		 * @return     : String
		 * @author     : jiayiwu
		 * @time       : 2017年8月30日 下午6:05:21
		 * @throws
	 */
	@RequestMapping(value="/view/new",method=RequestMethod.GET)
	public String newObjectInfo(Model model){
		
		IotParkingURIProjectMapping parkingURIProjectMapping = new IotParkingURIProjectMapping();
		
		List<IotParkingURI> iotParkingURIList = iotParkingURIService.list();
		List<SysProject> sysProjectList = sysProjectService.list();

		model.addAttribute("parkingURIProjectMapping",parkingURIProjectMapping);
		model.addAttribute("iotParkingURIList",iotParkingURIList);
		model.addAttribute("sysProjectList",sysProjectList);
		model.addAttribute("typeCode",1);
		
		return "iotInterface/parkingURI/parkingURIPrejectMappingFrom";
	}

	/**
	 * 
		 * @Title      : editObjectInfo
		 * @Description:  TODO
		 * @param      :@param id
		 * @param      :@param model
		 * @param      :@return
		 * @return     : String
		 * @author     : jiayiwu
		 * @time       : 2017年8月30日 下午6:06:20
		 * @throws
	 */
	@RequestMapping(value="/view/edit",params="id",method=RequestMethod.GET)
	public String editObjectInfo(@RequestParam int id,Model model){
			
		IotParkingURIProjectMapping parkingURIProjectMapping = iotParkingURIProjectMappingService.getIotParkingURIProjectMappingById(id);
		
		List<IotParkingURI> iotParkingURIList = iotParkingURIService.list();
		List<SysProject> sysProjectList = sysProjectService.list();

		model.addAttribute("parkingURIProjectMapping",parkingURIProjectMapping);
		model.addAttribute("iotParkingURIList",iotParkingURIList);
		model.addAttribute("sysProjectList",sysProjectList);
		model.addAttribute("typeCode",2); 
		
		return "iotInterface/parkingURI/parkingURIPrejectMappingFrom";
	}
	
	/************************** data controller ***************************/
	/**
	 * 
		 * @Title      : getAllData
		 * @Description:  TODO
		 * @param      :@param response
		 * @param      :@return
		 * @return     : String
		 * @author     : jiayiwu
		 * @time       : 2017年8月30日 下午6:41:07
		 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/getAllData", method = RequestMethod.POST,produces={"text/html;charset=UTF-8"})
	private String getAllData(HttpServletResponse response) {
		objectMapper.addMixInAnnotations(IotParkingURIProjectMapping.class, IotParkingURIProjectMappingMixin.BasicInfo.class);
		List<IotParkingURIProjectMapping> iotParkingURIProjectMappingList = iotParkingURIProjectMappingService.list();
		try {
			return objectMapper.writeValueAsString(iotParkingURIProjectMappingList);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 
	 * @Title		:getIotParkingURIProjectMappingByKey
	 * @Description	:TODO
	 * @param		:@param key
	 * @param		:@return
	 * @return		:List<IotParkingURIProjectMapping>
	 * @author		:Administrator
	 * @time		:2017年9月7日 下午1:38:46
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/getIotParkingURIProjectMappingByKey", method = RequestMethod.POST)
	private List<IotParkingURIProjectMapping> getIotParkingURIProjectMappingByKey(@RequestParam String key) {
		List<IotParkingURIProjectMapping> list = iotParkingURIProjectMappingService.listIotParkingURIProjectMappingByKey(key);
		return list;
	}
	
	/**
	 * 
	 * @Title		:getIotParkingURIProjectMappingById
	 * @Description	:TODO
	 * @param		:@param id
	 * @param		:@return
	 * @return		:IotParkingURIProjectMapping
	 * @author		:Administrator
	 * @time		:2017年9月7日 下午1:39:30
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/getIotParkingURIProjectMappingById", method = RequestMethod.POST)
	private IotParkingURIProjectMapping getIotParkingURIProjectMappingById(@RequestParam Integer id) {
		IotParkingURIProjectMapping obj = iotParkingURIProjectMappingService.getIotParkingURIProjectMappingById(id);
		return obj;
	}
	
	/**
	 * 
	 * @Title		:newObjectInfo
	 * @Description	:TODO
	 * @param		:@param iotParkingURIProjectMapping
	 * @param		:@return
	 * @return		:MessageObject
	 * @author		:Administrator
	 * @time		:2017年9月7日 下午6:21:05
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/new", method = RequestMethod.POST)
	private MessageObject newObjectInfo(@RequestBody IotParkingURIProjectMapping iotParkingURIProjectMapping) {
		MessageObject messageObject = new MessageObject();
		try {
			iotParkingURIProjectMappingService.save(iotParkingURIProjectMapping);
			messageObject.setMessage("添加成功 !");
			messageObject.setStatus(true);
		} catch (Exception e) {
			e.printStackTrace();
			messageObject.setMessage("添加失败 !");
			messageObject.setStatus(false);
		}
		
		return messageObject;
	}
	/**
	 * 
	 * @Title		:editObjectInfo
	 * @Description	:TODO
	 * @param		:@param iotParkingURIProjectMapping
	 * @param		:@return
	 * @return		:MessageObject
	 * @author		:Administrator
	 * @time		:2017年9月7日 下午1:44:58
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	private MessageObject editObjectInfo( @RequestBody IotParkingURIProjectMapping iotParkingURIProjectMapping) {
		MessageObject messageObject = new MessageObject();
		try {
			iotParkingURIProjectMappingService.update(iotParkingURIProjectMapping);
			messageObject.setMessage("编辑成功 !");
			messageObject.setStatus(true);
		} catch (Exception e) {
			e.printStackTrace();
			messageObject.setMessage("编辑失败 !");
			messageObject.setStatus(false);
		}
		
		return messageObject;
	}
	/**
	 * 
	 * @Title		:delObjectInfo
	 * @Description	:TODO
	 * @param		:@param ids
	 * @param		:@return
	 * @return		:MessageObject
	 * @author		:Administrator
	 * @time		:2017年9月7日 下午1:44:52
	 * @throws
	 */
	@RequestMapping(value="/del", method=RequestMethod.POST)
	@ResponseBody
	private MessageObject delObjectInfo(@RequestParam("id") String ids) {
		MessageObject messageObject = new MessageObject();
		String[] strs = ids.split(",");
		List<Integer> idArray = new ArrayList<Integer>();
		for(String str : strs){
			idArray.add(Integer.parseInt(str));
		}
		try {
			iotParkingURIProjectMappingService.delete(idArray);
			messageObject.setMessage("删除成功!");
			messageObject.setStatus(true);
		} catch (Exception e) {
			// TODO: handle exception
			messageObject.setMessage("删除失败!");
			messageObject.setStatus(false);
		}
		return messageObject;
	}
	
}
